<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// +----------------------------------------------------------------------
// | 网站后台配置管理-网站配置分配
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// | 2015-6-26上午10:56:05
// +----------------------------------------------------------------------

class Config extends MY_Controller{
	
	private $group;//配置分组
	private $type;//配置类型
	public function __construct(){
		parent::__construct();
		$this->group = array('1'=>'基本','2'=>'内容','3'=>'用户','4'=>'系统');
		$this->type  = array('0'=>'数字','1'=>'字符','2'=>'文本','3'=>'数组','4'=>'枚举');
		$this->load->helper('common');//加载公共函数
	}
	
	
	/**
	 * 网站配置后台首页
	 * @author Baip 125618036@qq.com
	 */
	public function index(){
		//检测如果非开发模式不能操作
		$this->checkDev();
		$where = array();
		//搜索
		$search_name = $this->input->get_post('search_name');
		if ( !empty( $search_name ) ){
			$where = "name LIKE '%$search_name%' OR title LIKE '%$search_name%' " ;
			$suffix = urldecode( '?search_name='.$search_name );//如果url中除了页码参数还有其它参数时
		}
		//获取不同的公组数据
		$type = intval ( $this->input->get('type') );
		if ( $type > 0 ){
			$where = array('group'=>$type);
			$suffix = urldecode( '?type='.$type );//如果url中除了页码参数还有其它参数时
		}
		
		//载入网站配置模型 
		$this->load->model('Config_model','con');
		$total = $this->con->getCount($where);//获取数据记录数
		
		//分页
		$this->load->library('pagination');//载入分页类
		$page_size = 10;//每页显示的记录数
		$url_segment = $this->uri->segment(3);//获取url中的第三段为当前页码
		//获取分页配置
		$suffix = empty( $suffix ) ? '' : $suffix;
		$config = get_page_config(site_url('config/index'),$total, $page_size, 3, $suffix, site_url('/config/index/1/').$suffix);
		$this->pagination->initialize($config);
		$field="id,title,name,group,type";
		$offset = intval( $url_segment );//获取当前页码
		//获取偏移数，如果当前页码大于0，用当前页码－1*每页记录数
		$offset = $offset > 0 ? ( $offset - 1  ) * $page_size : $offset;
		$list = $this->con->getData($where, $field, $page_size, $offset);//获取数据
		$pages = $this->pagination->create_links();//显示分页
		
		$this->load->vars('list', $list);//数据
		$this->load->vars('pages', $pages);//分页输出
		$this->load->vars('group_type', array('group'=>$this->group, 'type'=>$this->type));//分组
		$this->load->vars('record_count', $total);//记录部数
		$this->load->vars('meta_title', '配置管理');
		$this->load->view('config/index');
	}
	
	/**
	 * 添加配置内容
	 */
	public function create(){
		//检测如果非开发模式不能操作
		$this->checkDev();
		//引入表单验证类
		$this->load->library('form_validation');
		//验证表单及是post提交的时候才可以入库
		if( $this->form_validation->run('config') && IS_POST){
			$this->load->model('Config_model','con');//载入config模型
			$insert_id = $this->con->addData();
			if ( $insert_id ){
				//载入缓存并清除缓存
				$this->load->driver('cache',array('adapter' => 'apc', 'backup' => 'file'));//载入缓存,以文件形式存储
				$this->cache->delete('config_data');
				$this->show_message('success','添加成功');
			}else{
				$this->show_message('error','添加失败');
			}
				
		}else{
			$this->load->vars('meta_title', '新增配置');
			$this->load->vars('group', $this->group);//分组
			$this->load->vars('type', $this->type);//分组
			$this->load->view('config/create');
		}
	}
	
	/**
	 * 配置状态修改（修改状态，删除数据）
	 */
	public function changestats(){
		//检测如果非开发模式不能操作
		$this->checkDev();
		$method = $this->uri->segment(3);
		//判断是post方式还是get方式
		if ( IS_POST ){
			$id = $this->input->post('id', TRUE);//post接收删除的id值
		}else{
			$id = intval( $this->uri->segment(4) );
		}
		if ( empty( $method ) && $id <= 0 ){
			$this->show_message('error', '参数错误', site_url('config/index') );
		}
		
		//根据不同的请求方式操作不同的机制
		$this->load->model('Config_model', 'con');
		switch ($method){
			case 'm_delete':
				$state = $this->con->deleteData('id', $id);
			break;
		}
		
		if ( $state ){
			ajaxreturn(1,'操作成功', site_url('config/index'));
		}else{
			ajaxreturn(0,'操作失败', site_url('config/index'));
		}
		
	}
	
	/**
	 * 修改配置管理
	 * @param unknown_type $id
	 */
	public function modfiy($id){
		//检测如果非开发模式不能操作
		$this->checkDev();
		$id = intval( $id );
		if ( $id <= 0 ){
			$this->show_message('error', '参数错误');
		}
		//引入表单验证类
		$this->load->library('form_validation');
		//验证表单及是post提交的时候才可以入库
		if( $this->form_validation->run('config') && IS_POST){
			$this->load->model('Config_model','con');//载入config模型
			$update_bool = $this->con->updateData(array('id'=>$id));
			if ( $update_bool ){
				//载入缓存并清除缓存
				$this->load->driver('cache',array('adapter' => 'apc', 'backup' => 'file'));//载入缓存,以文件形式存储
				$this->cache->delete('config_data');
				$this->show_message('success','修改成功', site_url('config/index'));
			}else{
				$this->show_message('error','修改失败');
			}
		
		}else{
			//获取修改数据
			$this->load->model('Config_model','con');
			$info = $this->con->getInfo('id', $id);

			$this->load->vars('meta_title', '新增配置');
			$this->load->vars('group', $this->group);//分组
			$this->load->vars('type', $this->type);//分组
			
			$this->load->vars('info', $info);
			$this->load->vars('meta_title', '修改配置');
			$this->load->view('config/modfiy');
		}
	}
	
	/**
	 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	 * 开放功能模块
	 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	 */
	
	/**
	 * 网站设置
	 */
	public function webset(){
		$group = intval( $this->uri->segment(3,0) );
		$group = $group == 0 ? 1 : $group;
		
		//获取数据
		$map = array('group'=> $group);
		$this->load->model('Config_model', 'con');
		$record_count = $this->con->getCount($map);
		$field = 'id,name,title,value,type,remark,extra,group';
		$config_list = $this->con->getData($map, $field, $record_count);
		
		$this->load->vars('_list', $config_list);
		$this->load->vars('group', $group);
		$this->load->view('config/webset',$group);
	}
	/**
	 * 保存网站设置数据入库
	 */
	public function webseted(){
		if ( IS_POST ){
			$this->load->model('Config_model', 'con');
			$update_booler = $this->con->updateDataConfig();
			if ( $update_booler ){
				//载入缓存并清除缓存
				$this->load->driver('cache',array('adapter' => 'apc', 'backup' => 'file'));//载入缓存,以文件形式存储
				$this->cache->delete('config_data');
				$this->show_message('success', '更新成功', site_url('config/webset'));
			}
			
		}else{
			$this->show_message('error','非法请求');
		}
	}
	
}